﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpaceSquirrel.Automation
{
	public class PowerUpAutomation : SpriteAutomation
	{
		private int counter = 0;
		public bool visible = true;

		public PowerUpAutomation(Sprite sprite)
			: base(sprite)
		{ }

		public override void DoStuff(PlayScene scene)
		{
			Sprite player = scene.Player;
			if (this.sprite.powerUpInfo.ID == null)
			{
				this.counter++;
				if (this.counter > 8 * 30)
				{
					this.sprite.dead = true;
				}
				else if (this.counter > 6 * 30)
				{
					this.visible = (this.counter & 1) == 0;
				}
			}

			foreach (int playerY in new int[] { player.y, player.y - 16 })
			{
				int x = player.x;
				int y = playerY;
				int dx = this.sprite.x - x;
				int dy = this.sprite.y - y;
				if (dx * dx + dy * dy < 144)
				{
					this.ApplyPowerUp(scene.Context);
				}
			}
		}

		private void ApplyPowerUp(Context context)
		{
			this.sprite.dead = true;
			PowerUp info = this.sprite.powerUpInfo;
			string id = info.ID;
			string type = info.Type;
			if (id != null)
			{
				context.PowerUpsTaken[id] = true;
			}
			if (type == "life_small")
			{
				SoundManager.PlayNoise("raise_health");
				context.LifeMeter += 2;
				if (context.LifeMeter > 10)
				{
					context.LifeMeter = 10;
				}
			}
			else if (type == "life_big")
			{
				SoundManager.PlayNoise("raise_health");
				context.LifeMeter = 10;
			}
			else if (type == "1up")
			{
				SoundManager.PlayNoise("1up");
				context.Lives += 1;
			}
		}
	}
}
